package sv;

import javax.swing.JOptionPane;

public class SolverManager implements Runnable {
	private int iNS = 0, fin=0;
	private int[] nS;
	private ToolBox tb;
	private WelcomeWindow wWindow;
	private boolean show;

	public SolverManager(WelcomeWindow ww,int[] solvers,boolean vis,int rit) {
		wWindow = ww;
		nS=solvers;
		show=vis;
		ToolBox.setRit(rit);
		tb = new ToolBox(SudokuGames.get(0), true);
		for (int i = 0; i < solvers.length; i++) {
			Thread solver = new Thread(this);
			solver.start();
		}
	}

	public void run() {
		int nS = getNs();
		// TODO Auto-generated method stub
		for (int i = 1; i < SudokuGames.total() + 1 && !tb.isForce(); i++) {
			if (nS < 2) {
				Solver solver = new Solver(nS, i - 1, true, show);
			} else {
				SolverRule solver = new SolverRule(i - 1, true, show);
			}
			tb.resetOver(nS);
			tb.resetPassi(nS);
			if (i < SudokuGames.total())
				tb.inizializzazione(SudokuGames.get(i), nS);
		}
		System.out.println(nS + "-min: " + tb.getMin(nS) + " max:"
				+ tb.getMax(nS));
		fine();
	}

	public synchronized int getNs() {
		return nS[iNS++];
	}
	
	public synchronized void fine() {
		fin++;
		if(fin==nS.length){
			System.out.println("Finito");
			for(int i=0;i<3;i++){
				System.out.println(i + "-min: " + tb.getMin(i) + " max:"
						+ tb.getMax(i));
			}
			tb.finito();
			if(!tb.isForce())
				JOptionPane.showMessageDialog(null, "Test finito");
			else
				JOptionPane.showMessageDialog(null, "Test interrotto");
			tb.closeGraph();
			tb.resetForce();
			wWindow.setVisible(true);
		}
	}

}
